/*
 * ////开发区域经销商报价 不用了
 */
public without sharing class QuoteDealerController
{
    public List<ProductBean> pbList { get; set; }//产品列表
    public List<QuoteProductBean> qpbList { get; set; }///报价产品列表
    public Quote q { get; set; }//报价
    
    
    public String qId { get; private set; }  // 报价Id
    public String oppId { get; private set; }  // 业务机会Id
    public String type { get; private set; }///类型
    
    public List<SelectOption> cate0Opts { get; private set; }
    public List<SelectOption> cate1Opts { get; private set; }
    
    //搜索用的字段
    public String name { get; set; }
    public String brand { get; set; }
    public String serial { get; set; }
    public String category0 { get; set; }
    public String category1 { get; set; }
    public String typeName {get;set;}
    
    public User u;
    private Map<Id,PriceBean> prMap ;//价格
    public Boolean Approve {get;set;}
    
    
    public Boolean isOption {get;set;} //是否为操作状态
    public List<QuoteProductBean> tempList { get; set; }///报价产品列表 临时操作的
    public String tempName {get;set;}//名称
    public String tempCategory1 {get;set;}//一级分类
    public String tempCategory2 {get;set;}//二级分类
    public String tempBrand {get;set;}//品牌
    public String tempTypeName {get;set;}//品牌
    public List<SelectOption> tempCategory1opts { get; private set; }//一级分类选项列表
    public List<SelectOption> tempCategory2opts { get; private set; }//二级分类选项列表
    public Boolean isSpecialAccount {get;set;}//是否为专票客户
    public Boolean isSaveSuccess {get;set;}//是否保存成功
    
    public QuoteDealerController(ApexPages.StandardController stdController)
    {
        qId = stdController.getId();
        qId = qId == null ? System.currentPageReference().getParameters().get('qid'): qId;
        oppId = System.currentPageReference().getParameters().get('oppid');
        pbList  = new List<ProductBean>();
        prMap = new Map<Id ,PriceBean>();
        qpbList = new List<QuoteProductBean>();
        tempList = new List<QuoteProductBean>();
        isSaveSuccess = false;
        
        cate0Opts = new List<SelectOption>();
        cate0Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category0.split(',', -1)) {
            cate0Opts.add(new SelectOption(str, str));
        }

        cate1Opts = new List<SelectOption>();
        cate1Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category1.split(',', -1)) {
            cate1Opts.add(new SelectOption(str, str));
        }
    }
    public void init(){
        if(Test.isRunningTest()){
        
        }else{
        u = [select SubCompany__c from User where Id = :UserInfo.getUserId()];
        }
    if(String.isBlank(qId) == false){///如果报价Id不为空 则为编辑页面
        getQuoteProduct();
            
            if(q.Status=='申请中' || q.Status=='已批准')
            {
                
                Approve = true;
               // ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'此报价已审批，禁止修改'));
            }
        }
        if(String.isBlank(oppId) == false){///如果业务机会不为空 则为新建页面
            q = new Quote();
            q.OpportunityId = Id.valueOf(oppId);
            q.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DealerQuote','Quote');
            getQuoteProductLastYear();
        }
        ///设置筛选下拉框
        tempCategoryOpt(qpbList);        
    }
    ///查找上年度合同,如果有则带出合同产品如果没有则什么都不做
    private void getQuoteProductLastYear(){
        ///查找客户
    Opportunity opp= [select Account.Id,Account.Name,Account.BillingType__c from Opportunity where Id =:oppId];
        ///客户是否为专票客户
        if(opp.Account.BillingType__c != null){
        isSpecialAccount = opp.Account.BillingType__c.contains('专票'); 
        }
         
        String lastYear = String.valueOf(Date.today().year() - 1) + '年经销商年度合同';  //去年年份 
        if(Test.isRunningTest()){
        lastYear = String.valueOf(Date.today().year()) + '年经销商年度合同'; 
        }
        q.Name = opp.Account.Name + '报价-' + Utils.date2string(Date.today(), 'YYYYMMDD'); 

        ///查找上年合同
        List<Contract__c> cList = [select Id from Contract__c where Account__c =:opp.Account.Id and YearContract__c =:lastYear];
        ///如果存在上年的年度合同则带出合同产品及价格
        if( cList.size() > 0 ){
            
            Contract__c c = cList[0];
           ///转换为报价产品
            Set<Id> pidList = new Set<Id>();
            List<ContractProduct__c> cpList = [select 
            name,Product__r.Name,Product__r.SerialNo__c,Product__r.Spec__c,Product__r.Brand__c,Product__r.BrandShort__c,Product__r.NCCode__c,
            Product__r.Unit__c,Product__r.Model__c, Product__r.ShortnameEng__c ,Product__r.FullnameEng__c ,Product__r.FullnameChs__c ,
            Product__r.Category0__c,Product__r.Category1__c,Product__r.Category2__c,Product__r.TypeName__c,
            Product__r.Category3__c,UnitPrice__c,SalesPrice__c,Product__c,Product__r.zl__c                         
                                          from ContractProduct__c where Contract__c =:c.Id];
            ///
            for(ContractProduct__c cp : cpList ){
               pidList.add(cp.Product__c); 
            }
            //获取价格
            Map<Id,Price__c> priceMap = getPriceMap(pidList);
            ///生成报价
            for(ContractProduct__c cp : cpList ){
                 qpbList.add(new QuoteProductBean(cp,priceMap.get(cp.Product__c)));
            }
            
        }
    
    }

    ///如果是编辑则带出 已经有的报价产品
    private void getQuoteProduct(){
        System.debug(qid);
        q = [select Id, Status,Name ,Comment__c ,OpportunityId from Quote where id =:qId ];
        Opportunity opp = [select Account.BillingType__c from Opportunity where id =:q.OpportunityId];
         ///客户是否为专票客户
            if(opp.Account.BillingType__c != null){
                isSpecialAccount = opp.Account.BillingType__c.contains('专票'); 
            }
 Schema.DescribeSobjectResult m = QuoteProduct__c.sObjectType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();

                String sql = 'select ';
                String fields = '';
                for (String field : fieldMap.keySet()) {
                    if (fields.length() > 0) {
                        fields += ', '; 
                    }
                    fields += field;
                }
                sql += fields;
                sql += ' , Product__r.NCCode__c,Product__r.zl__c,Product__r.TypeName__c from QuoteProduct__c where Quote__c = \'' + qId + '\'';
                for (QuoteProduct__c qp : Database.query(sql)) {
                    qpbList.add(new QuoteProductBean(qp));
                }        
    }
    
    ///检索产品 并带出价格
     public void search() {
         pbList.clear();
        String whereStr = '';
        List<Product__c> plist = new List<Product__c>();
        if (String.isBlank(name) == false) {
            whereStr += ' and Name like \'%' + name + '%\'';
        }
        if (String.isBlank(brand) == false) {
            whereStr += ' and  Brand__c like \'%' + brand + '%\'';
        }
        if (String.isBlank(serial) == false) {
            whereStr += ' and  SerialNo__c like \'%' + serial + '%\'';
        }
        if (String.isBlank(category0) == false) {
            whereStr += ' and  Category0__c = \'' + category0 + '\'';
        }
        if (String.isBlank(category1) == false) {
            whereStr += ' and  Category1__c = \'' + category1 + '\'';
        }
         if (String.isBlank(typeName) == false) {
            whereStr += ' and  TypeName__c like \'%' + typeName + '%\'';
        }
               
        plist = Database.query(this.soqlStr() + whereStr + ' limit 500');
       // 获取价格信息
        Set<Id> pidList = new Set<Id>();
        for (Product__c p : plist) {
            pidList.add(p.Id);
        }
        Map<Id, Price__c> priceMap = getPriceMap(pidList);

        for (Product__c p : plist) {
            pbList.add(new ProductBean(p, priceMap.get(p.Id)));
        }
         
        
    }
     private String soqlStr() {
        Schema.DescribeSobjectResult m = Product__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' from Product__c where Name != null';

        return sql;
    }
      
///获取价格
  private Map<Id,Price__c> getPriceMap(Set<Id> pids){
  Map<Id, Price__c> priceMap = new Map<Id, Price__c>();
            for (Price__c pr : [select Id, Product__c,Product__r.bggsjhdj__c, MarketPrice__c, HospitalManagerPrice__c, HospitalBottomPrice__c, CompanyBottomPrice__c,
                AreaDealerPrice__c, InvoiceDealerPrice__c, RepairCost__c, InstCost__c ,LastMonthAveragePrice__c,Taxrate__c  
                                from Price__c where Product__c in : pids and Company__c = :u.SubCompany__c]) {
                priceMap.put(pr.Product__c, pr);
            }
      return priceMap;
  
  }
    ////获取上月平均价
  private Map<Id,AveragePrice__c> getAveragePriceMap(Set<Id> pids){
           /// 日期处理
                Integer m = Date.today().month(); //当前月份
                Integer y = Date.today().year();  //当前年份 
               ///取上月
                 if(m == 1){
                m = 12 ;
                    y -= 1;
                }else{
                m -= 1;

                }

                String  month = String.valueOf(m) + '月';
                String  year= String.valueOf(y) + '年';
            ///获取上月平均价
            Map<Id,AveragePrice__c>aPriceMap = new Map<Id,AveragePrice__c>();
            for(AveragePrice__c a :[select Id ,AveragePrice__c,Product__c from AveragePrice__c 
                                    where Product__c in : pids and Company__c = :u.SubCompany__c and Year__c =:year and Month__c =:month]){
            aPriceMap.put(a.Product__c,a);
            }
      return aPriceMap;
  }
  public PageReference save1() {
        Savepoint sp = Database.setSavepoint();
        try {
            // 必须有产品才能保存
            if (qpbList.size() == 0) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请选择产品'));
                return null;
            }
            // 报价upsert
            upsert q;

            String accid = [select AccountId from Quote where Id = :q.Id].AccountId;

            // 获取价格信息
            Set<Id> pIdList = new Set<Id>();
            for (QuoteProductBean qpb : qpbList) {
                pIdList.add(qpb.productId);
            }
            Map<Id, Price__c> priceMap = getPriceMap(pIdList);
                
            ///获取上月平均价
            Map<Id,AveragePrice__c>aPriceMap = getAveragePriceMap(pIdList);

            Boolean noPrice = false;
            String errMsg = '';
            for (QuoteProductBean qpb : qpbList) {
                // 如果该产品没有对应的价格体系存在，提示错误信息
                if (priceMap.containsKey(qpb.productId) == false) {
                    noPrice = true;
                    // **产品（货号：*；存货编码：*）没有价格体系，请与产品经理联系
                    errMsg = qpb.name + '(' + Schema.Product__c.fields.SerialNo__c.getDescribe().getLabel() + ':' + qpb.serialNo + '; '
                             + Schema.Product__c.fields.NCCode__c.getDescribe().getLabel() + ':' + qpb.ncCode + ') '
                             + System.Label.Err_NoPrice;
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errMsg));
                }else{
                Price__c p = priceMap.get(qpb.productId);
                   if(p.InstCost__c == null){
                    noPrice = true;
                         errMsg = qpb.name +'字段 \''+ Schema.Price__c.fields.InstCost__c.getDescribe().getLabel()+ '\'为空! 请联系系统管理员进行维护!';
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errMsg));
                    }
                }
            }
            // 有错就return
            if (noPrice == true) {
                Database.rollback(sp);
                return null;
            }

            // 报价产品delete
            List<QuoteProduct__c> delList = [select Id from QuoteProduct__c where Quote__c = :qId];
            if (delList.size() > 0) delete delList;

            // 报价产品insert
            List<QuoteProduct__c> insList = new List<QuoteProduct__c>();
            for (QuoteProductBean qpb : qpbList) {
                insList.add(
                    new QuoteProduct__c(
                        Name = qpb.name,
                        AreaDealerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).AreaDealerPrice__c : 0,
                        InvoiceDealerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InvoiceDealerPrice__c : 0,
                        // HospitalSalesPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalSalesPrice__c : 0,
                        HospitalManagerPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalManagerPrice__c : 0,
                        HospitalBottomPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).HospitalBottomPrice__c : 0,
                        CompanyBottomPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).CompanyBottomPrice__c : 0,
                        LastMonthAveragePrice__c = aPriceMap.containsKey(qpb.productId) ? aPriceMap.get(qpb.productId).AveragePrice__c : 0,
                         ///成本价
                        InstCost__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InstCost__c : 0,
                     //   InstCost__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).InstCost__c : 0,
                        
 UnitPrice__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).MarketPrice__c : 0,///市场报价
                        SalesPrice__c = String.isBlank(qpb.salesPrice) ? 0 : Decimal.valueOf(qpb.salesPrice),///售价
                        
                        Quote__c = q.Id,
                        Product__c = qpb.productId,
                        Unit__c = qpb.unit,
                        Category2__c = qpb.category2,
                        Spec__c = qpb.spec,
                        SerialNo__c = qpb.serialNo,
                        Brand__c = qpb.brand,
                        BrandShort__c = qpb.brandS,
                        Category3__c = qpb.category3,
                        Quantity__c = String.isBlank(qpb.quantity) ? 0 :Integer.valueOf(qpb.quantity),
                        Category4__c = qpb.category4,
                        Model__c = qpb.model,
                        Category1__c = qpb.category1,
                        ShortnameEng__c = qpb.sNameEng,
                        FullnameEng__c = qpb.fnameEng,
                        FullnameChs__c = qpb.fNameChs,
                        Category0__c = qpb.category0,
                        RepairCost__c = priceMap.containsKey(qpb.productId) ? priceMap.get(qpb.productId).RepairCost__c : 0,
                       
                        DiscountRate__c = String.isBlank(qpb.discountRate) ? 0 : Decimal.valueOf(qpb.discountRate),
                        Account__c = accid,
                        
                        
                        WarrantyPeriod__c = qpb.qp.WarrantyPeriod__c,
                        ChargingStandard__c = qpb.qp.ChargingStandard__c,
                        ResponseHours__c = qpb.qp.ResponseHours__c,
                        Period__c = qpb.qp.Period__c,
                        ApplicationTraining__c = qpb.qp.ApplicationTraining__c, 
                        EquipmentCheck__c = qpb.qp.EquipmentCheck__c,
                        EquipmentCheckGoods__c = qpb.qp.EquipmentCheckGoods__c,
                        CheckPeriod__c= qpb.qp.CheckPeriod__c,
                        Consumables__c = qpb.qp.Consumables__c,
                        RapidWear__c = qpb.qp.RapidWear__c,
                        SpecialRequirement__c = qpb.qp.SpecialRequirement__c,
                        Taxrate__c = qpb.taxRate
                                                                   
                    )
                );
            }
            insert insList;
            PageReference ref = new PageReference('/' + q.Id);
            ref.setRedirect(true);
            isSaveSuccess = true;
            return ref;
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, System.Label.Save_OK));
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }
        return null;
    }
    
   
     public void exchange() {
        // 去掉已勾选的报价产品
        List<QuoteProductBean> stayList = new List<QuoteProductBean>();
        for (QuoteProductBean qpb : qpbList) {
            if (qpb.chk == false) {
                stayList.add(qpb);
            }
        }
        qpbList.clear();
        qpbList.addAll(stayList);

        // 收集已选报价产品的产品Id
        Set<Id> existPid = new Set<Id>();
        for (QuoteProductBean qpb : qpbList) {
            existPid.add(qpb.productId);
        }
         
        // 加入已勾选的产品
        for (ProductBean p : pbList) {
            if (p.chk == true && existPid.contains(p.rec.Id) == false) {
                qpbList.add(new QuoteProductBean(p.rec,p.pr));
                p.chk = false;
            }
        }
         tempCategoryOpt(qpbList);

    }
    ///删除操作
    public void deleteOption(){
        isOption = false;
        List<QuoteProductBean> tList = new List<QuoteProductBean>();
        tList.addAll(qpbList);
        Map<Id,Id> deleteID = new Map<Id,Id>();
    for(QuoteProductBean pb : tempList){
            if(pb.chk == true){
               deleteID.put(pb.productId,pb.productId);
             }
            
         }
        qpbList.clear();
for(QuoteProductBean qpb : tList){
                 if(deleteID.containskey(qpb.productId)){
                 continue;
                 }else{
                 qpbList.add(qpb);
                 }
                            
             }  
       tempCategoryOpt(qpbList);

    }
     
    ///保存更新操作
    public void saveOption(){
        isOption = false;
        List<QuoteProductBean> tList = new List<QuoteProductBean>();
        tList.addAll(qpbList);
        Map<Id,QuoteProductBean> updateqpb = new Map<Id,QuoteProductBean>();
    for(QuoteProductBean pb : tempList){
               updateqpb.put(pb.productId,pb);            
         }
        qpbList.clear();
for(QuoteProductBean qpb : tList){
                 if(updateqpb.containskey(qpb.productId)){
                 qpbList.add(updateqpb.get(qpb.productId));
                 }else{
                 qpbList.add(qpb);
                 }
                            
             } 
         tempCategoryOpt(qpbList);

    }
   public void cancelOption(){
        isOption = false;
        tempList.clear();
       tempCategoryOpt(qpbList);
   }
public void shaixuan(){
        isOption = true;
    tempList.clear();
        Map<Id,QuoteProductBean> temp = new Map<Id,QuoteProductBean>();
        ///名称
        if(String.isBlank(tempName) == false){
        for(QuoteProductBean pb : qpbList){
            if(pb.name.contains(tempName)){
                temp.put(pb.productId ,pb);
                }            
            }
        }else{
        temp = setTempMap();
        }
        ///一级分类
        Map<Id,QuoteProductBean> temp1 = new Map<Id,QuoteProductBean>();
        if(String.isBlank(tempCategory1) == false){
           if(temp.size() == 0)temp = setTempMap();

        for(QuoteProductBean pb : temp.values()){
            if(pb.category1.contains(tempCategory1)){
                temp1.put(pb.productId ,pb);
                }
            
            }
        }else{
        temp1 = temp;
        }

        ///二级分类
        Map<Id,QuoteProductBean> temp2 = new Map<Id,QuoteProductBean>();
        if(String.isBlank(tempCategory2) == false){
       if(temp1.size() == 0)temp1 = setTempMap();
        for(QuoteProductBean pb : temp1.values()){
            if(pb.category1.contains(tempCategory2)){
                temp2.put(pb.productId ,pb);
                }
            
            }
        }else{
        temp2 = temp1;
        }
        //三级分类
        Map<Id,QuoteProductBean> temp3 = new Map<Id,QuoteProductBean>();
        if(String.isBlank(tempBrand) == false){
       if(temp2.size() == 0)temp2 = setTempMap();
        for(QuoteProductBean pb : temp2.values()){
            if(pb.brand.contains(tempBrand) || pb.brandS.contains(tempBrand)  ){
                temp3.put(pb.productId ,pb);
                }
            }
        }else{
        temp3 = temp2;
        }
        ///产品结构
    	 Map<Id,QuoteProductBean> temp4 = new Map<Id,QuoteProductBean>();
        if(String.isBlank(tempTypeName) == false){
       if(temp3.size() == 0)temp3 = setTempMap();
        for(QuoteProductBean pb : temp3.values()){
            if(pb.typeName.contains(tempTypeName) ){
                temp4.put(pb.productId ,pb);
                }
            }
        }else{
        temp4 = temp3;
        }
    
        tempList.addAll(temp4.values());
        
for(QuoteProductBean qpb:tempList){
        qpb.chk = true;
        }
        ///设置分类下拉框
       tempCategoryOpt(tempList);
        
    }
    private void tempCategoryOpt(List<QuoteProductBean> qbList){
     ///选项列表
        Set<String> c1 = new Set<String>();
        Set<String> c2 = new Set<String>();
for(QuoteProductBean qpb:qbList){
if(qpb.category1 !=null && qpb.category1 != '')c1.add(qpb.category1);
if(qpb.category2 !=null && qpb.category2 != '')c2.add(qpb.category2);
       }
        tempCategory1opts = optionHandler(c1);
      tempCategory2opts = optionHandler(c2);
    }
    ///设置选项列表
      private List<SelectOption>  optionHandler (Set<String> s){
       List<SelectOption> ss = new List<SelectOption>();
       ss.add(new SelectOption('', System.Label.Option_None));

        for (String str : s){
        ss.add(new SelectOption(str,str));
        }
        return ss;
    }
    ///
    public Map<Id,QuoteProductBean> setTempMap() {
        Map<Id,QuoteProductBean> temp = new Map<Id,QuoteProductBean>();
    for(QuoteProductBean qb: qpbList){
        temp.put(qb.productId,qb);
        }
        return temp;
    }
    
     public class QuoteProductBean {
        public String name { get; set; }
        public String serialNo { get; set; }
        public String ncCode { get; set; }
        public String spec { get; set; }
        public String brand { get; set; }
        public String brandS { get; set; }
        public String unit { get; set; }
        public String model { get; set; }
        public String sNameEng { get; set; }
        public String fnameEng { get; set; }
        public String fNameChs { get; set; }
        public String quantity { get; set; }
        public String category0 { get; set; }
        public String category1 { get; set; }
        public String category2 { get; set; }
        public String category3 { get; set; }
        public String category4 { get; set; }
        public Decimal marketPrice { get; set; }///市场报价
        public String discountRate { get; set; }
        public String salesPrice { get; set; } /// 售价
        public String comment { get; set; }
        public Id productId { get; set; }
        public Boolean chk { get; set; }
        public Decimal invoiceDealerPrice {get;set;}//报单经销商价
        public QuoteProduct__c qp {get;set;}//报价产品
        public Decimal taxRate {get;set;}//税率
        public Decimal lastYearSalesPrice {get;set;}//去年售价
        public Decimal zl {get;set;}//装量
        public String typeName {get;set;}//产品类型
       
        public QuoteProductBean() {}
///匹配数字
        private Decimal matchNumber(String str){
            if(str == null)return 0;
            Matcher m1 =  Pattern.compile('([1-9]([0-9]*)(\\.[0-9]+)?)|(^0$)').matcher(str);
            m1.find();
            try{
                String res = m1.group();
                return Decimal.valueOf(res);
            }catch(Exception e){
                return 0;
            }
        }
          public QuoteProductBean(QuoteProduct__c record) {
            qp = record;
            name = record.name;
            serialNo = record.SerialNo__c;
            ncCode = record.Product__r.NCCode__c;
            spec = record.Spec__c;
            brand = record.Brand__c;
            brandS = record.BrandShort__c;
            unit = record.Unit__c;
            model = record.Model__c;
            sNameEng = record.ShortnameEng__c;
            fnameEng = record.FullnameEng__c;
            fNameChs = record.FullnameChs__c;
            quantity = String.valueOf(record.Quantity__c);
            category0 = record.Category0__c;
            category1 = record.Category1__c;
            category2 = record.Category2__c;
            category3 = record.Category3__c;
            discountRate = String.valueOf(record.DiscountRate__c);
            salesPrice = String.valueOf(record.SalesPrice__c);//售价
            comment = record.Comment__c;
            productId = record.Product__c;
            taxRate = record.Taxrate__c;//税率
            invoiceDealerPrice = record.InvoiceDealerPrice__c;///报单经销商价
            marketPrice = record.UnitPrice__c;///市场报价 
            zl = matchNumber(record.Product__r.zl__c);//装量
            chk = false;
             typeName = record.Product__r.TypeName__c;
            
        }
     public QuoteProductBean(ContractProduct__c cp,Price__c pr) {
          name = cp.Product__r.Name;
            serialNo = cp.Product__r.SerialNo__c;
            ncCode = cp.Product__r.NCCode__c;
                spec = cp.Product__r.Spec__c;
                brand = cp.Product__r.Brand__c;
                brandS = cp.Product__r.BrandShort__c;
                unit = cp.Product__r.Unit__c;
                model = cp.Product__r.Model__c;
                sNameEng = cp.Product__r.ShortnameEng__c;
                fnameEng = cp.Product__r.FullnameEng__c;
                fNameChs = cp.Product__r.FullnameChs__c;
                category0 = cp.Product__r.Category0__c;
                category1 = cp.Product__r.Category1__c;
                category2 = cp.Product__r.Category2__c;
                category3 = cp.Product__r.Category3__c;
                productId = cp.Product__c;
            lastYearSalesPrice = cp.SalesPrice__c; ///售价带过来
                zl = matchNumber(cp.Product__r.zl__c);//装量

                chk = false;
              discountRate = '0';
            comment = null;
           typeName = cp.Product__r.TypeName__c;

            if(pr != null) {
                    marketPrice = pr.MarketPrice__c;
                    taxRate = pr.Taxrate__c;
                    invoiceDealerPrice = pr.InvoiceDealerPrice__c;
            }
              qp = new QuoteProduct__c();

                
          }

          public QuoteProductBean(Product__c record,Price__c pr) {
            name = record.Name;
            serialNo = record.SerialNo__c;
            ncCode = record.NCCode__c;
            spec = record.Spec__c;
            brand = record.Brand__c;
            brandS = record.BrandShort__c;
            unit = record.Unit__c;
            model = record.Model__c;
            sNameEng = record.ShortnameEng__c;
            fnameEng = record.FullnameEng__c;
            fNameChs = record.FullnameChs__c;
            category0 = record.Category0__c;
            category1 = record.Category1__c;
            category2 = record.Category2__c;
            category3 = record.Category3__c;
             typeName = record.TypeName__c;

            discountRate = '0';
            comment = null;
            if(pr != null) {
            marketPrice = pr.MarketPrice__c;
             taxRate = pr.Taxrate__c;
                invoiceDealerPrice = pr.InvoiceDealerPrice__c;
            }
            productId = record.Id;
            chk = false;
            zl = matchNumber(record.zl__c);//装量
            qp = new QuoteProduct__c();
        }
     }
        
    public class ProductBean {
public Product__c rec { get; set; }
        public Price__c pr { get; set; }
        public Boolean chk { get; set; }

        public ProductBean(Product__c record, Price__c price) {
            rec = record;
            pr = price;
            chk = false;
        }
    }
    public class PriceBean{
    public Id rec { get; set; }//产品
        public Price__c pr { get; set; }//价格
        public AveragePrice__c avpr {get;set;}//月平均价
    }
    
}